package com.wxw.BaseLibrary.component;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.wxw.BaseLibrary.R;
import com.wxw.BaseLibrary.adapter.SpinnerAdapter;
import com.wxw.BaseLibrary.drawable.PanelStyle;
import com.wxw.BaseLibrary.element.IListBoxItem;

import java.util.List;

/**
 * Created by david on 2014-07-11.
 */
public class ListBoxSpinnerItem implements IListBoxItem {
    private TextView tv_caption;
    private Spinner spinner;
    private ListBox parent;
    private String key;
    private boolean bChanged = false;
    private String caption = "";
    private String value = "";
    private int index = 0;
    private int[] BackgroundColor = new int[]{Color.parseColor("#ffffff")};
    private int BorderColor = Color.parseColor("#c7c7c7");
    private int BorderWidth = 1;
    private int radians = 3;
    private List<Object> data;
    private SpinnerAdapter adapter;
    private LinearLayout linearLayout;
    private ListBox.OnListBoxItemChangedListener listBoxItemChangedListener;

    public ListBoxSpinnerItem(ListBox parent, List<Object> objects) {
        this.parent = parent;
        data = objects;
    }

    @Override
    public View getView() {
        if (linearLayout==null) {
            initView();
        }
        return linearLayout;
    }

    public List<Object> getData() {
        return data;
    }

    public void setData(List<Object> data) {
        if (data != null) {
            this.data = data;
            adapter = new SpinnerAdapter(parent.getContext(), 0, this.data, 0);
            spinner = new Spinner(parent.getContext());
            spinner.setAdapter(adapter);
        }
    }

    public void setCaption(String caption){
        this.caption = caption;
        if (tv_caption != null) {
            tv_caption.setText(caption);
        }
    }

    public String getCaption() {
        return caption;
    }

    public void setSelectedIndex(int index) {
        this.index = index;
        if (spinner != null) {
            spinner.setSelection(index);
        }
    }

    public int getSelectedIndex(){
        index = spinner != null?spinner.getSelectedItemPosition():index;
        return index;
    }

    public void setValue(String value) {
        this.value = value;
        if (spinner != null && adapter != null && data != null) {
            int i = 0;
            for (Object o : data){
                if (o.toString().equals(value) || o.toString().startsWith(value)){
                    spinner.setSelection(i);
                    break;
                }
                i++;
            }
        }
    }

    public String getValue() {
        value = spinner != null?spinner.getSelectedItem().toString():value;
        return value;
    }

    @Override
    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public void setChanged(boolean b) {
        bChanged = b;
        adapter.setBackColor((bChanged ? new int[]{parent.getItemChangedBgColor()} : BackgroundColor));
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean isChanged() {
        return bChanged;
    }

    @Override
    public void setOnListBoxItemChangedListener(ListBox.OnListBoxItemChangedListener listener) {
        listBoxItemChangedListener = listener;
    }

    @Override
    public ListBox getParent() {
        return parent;
    }

    public Spinner getSpinner(){
        return spinner;
    }

    public ListBox getListBox() {
        return parent;
    }

    public int[] getBackgroundColor() {
        return BackgroundColor;
    }

    public void setBackgroundColor(int[] backgroundColor) {
        BackgroundColor = backgroundColor;
    }

    public int getBorderColor() {
        return BorderColor;
    }

    public void setBorderColor(int borderColor) {
        BorderColor = borderColor;
    }

    public int getBorderWidth() {
        return BorderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        BorderWidth = borderWidth;
    }

    public int getRadians() {
        return radians;
    }

    public void setRadians(int radians) {
        this.radians = radians;
    }

    private void initView(){
        linearLayout = new LinearLayout(parent.getContext());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//        linearLayout.setBackground(PanelStyle.createPanelStyle(null, BackgroundColor, BorderWidth, BorderColor, radians));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        layoutParams.setMargins(5, 10, 5, 10);
        linearLayout.setLayoutParams(layoutParams);

        tv_caption = new TextView(parent.getContext());
        tv_caption.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
        tv_caption.setBackgroundColor(Color.WHITE);
        tv_caption.setTextColor(Color.BLACK);
        tv_caption.setText(caption);
        linearLayout.addView(tv_caption, new LinearLayout.LayoutParams(this.parent.getCaptionWidth(), LinearLayout.LayoutParams.MATCH_PARENT));

        if (data != null) {
            adapter = new SpinnerAdapter(parent.getContext(), 0, data, 0);
            adapter.setBackColor(BackgroundColor);
            adapter.setBorderColor(BorderColor);
            adapter.setBorderWidth(BorderWidth);
            adapter.setCornerRadius(radians);
            adapter.setDropDownBackColor(BackgroundColor);
            adapter.setDropDownBorderColor(BorderColor);
            adapter.setDropDownBorderWidth(BorderWidth);
            adapter.setDownIconID(parent.getSpinnerDropDownIconID());
            spinner = new Spinner(parent.getContext());
            spinner.setAdapter(adapter);
        }
        spinner.setBackground(PanelStyle.createPanelStyle(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[]{Color.WHITE}, 0, 0, 0));

        spinner.setSelection(index);
        if (!"".equals(value.trim())) {
            int i = 0;
            for (Object o : data) {
                if (o.toString().equals(value.trim())) {
                    spinner.setSelection(i);
                    break;
                }
                i++;
            }
        }
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                triggerEvent(parent.getAdapter().getItem(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
//        editHelperClass.editText.setTextColor(getResources().getColor(R.color.cc_black));
//        spinnerHelperClass.spinner.se(hint);
        linearLayout.addView(spinner, layoutParams);
    }

    private void triggerEvent(Object value){
        if (listBoxItemChangedListener!=null && parent.isEditable()) {
            listBoxItemChangedListener.onItemChanged(this, value);
        }
    }
}
